import { computed } from 'vue';
import { useConfigStore } from '@renderer/stores/configStore'
import request from '@renderer/utils/request';

// 关键修改：移除顶级作用域的 Store 调用，改为在方法内部获取

// 辅助函数：获取基础 URL（延迟到调用时才执行）
const getBaseUrl = () => {
  // 此时调用 useConfigStore，确保在应用挂载后执行
  const configStore = useConfigStore();
  return configStore.baseUrl;
};

export function translateApi() {
  return {
    // 添加翻译记录
    addTranslate: (data: { en: string; cn: string; hit_count?: number }) => {
      return request({
        url: getBaseUrl() + 'sql/add_translate', // 调用辅助函数获取 URL
        method: 'post',
        data: data,
      });
    },

    // 批量添加翻译记录
    batchAddTranslate: (data: Array<{ en: string; cn: string; hit_count?: number }>) => {
      return request({
        url: getBaseUrl() + 'sql/batch_add_translate',
        method: 'post',
        data: { translations: data },
      });
    },

    // 删除翻译记录
    deleteTranslate: (data: { en: string }) => {
      return request({
        url: getBaseUrl() + 'sql/delete_translate',
        method: 'post',
        data: data,
      });
    },
    deleteTranslatesByFilter: (params: object) => {
      return request({
        url: getBaseUrl() + 'sql/delete_translates_by_filter',
        method: 'delete',
        params: params,
      })
    },

    // 更新翻译记录
    updateTranslate: (data: {
      en: string;
      cn?: string;
      hit_count?: number
    }) => {
      return request({
        url: getBaseUrl() + 'sql/update_translate',
        method: 'put',
        data: data,
      });
    },

    // 分页查询翻译记录
    getTranslatePaged: (params: {
      page?: number;
      page_size?: number;
      en?: string;
      cn?: string;
      hit_count_min?: number;
      hit_count_max?: number;
      sort_field?: string;
      sort_order?: 'ASC' | 'DESC';
    }) => {
      return request({
        url: getBaseUrl() + 'sql/get_translate_paged',
        method: 'get',
        params: params,
      });
    },

    // 导出翻译记录
    exportTranslateStream: (params: {
      en?: string;
      cn?: string;
      hit_count_min?: number;
      hit_count_max?: number;
      sort_field?: string;
      sort_order?: 'ASC' | 'DESC';
      taskId: string;
    }) => {
      return request({
        url: getBaseUrl() + 'sql/export_translate_stream',
        method: 'get',
        params: params,
        responseType: 'blob',
      });
    },

    // 批量翻译标签
    translateTags: (data: { tags: string[] }) => {
      return request({
        url: getBaseUrl() + 'translate/tags',
        method: 'post',
        data: data,
      });
    },

    // 批量更新命中次数
    updateHitCount: (data: { tags: string[] }) => {
      return request({
        url: getBaseUrl() + 'translate/update-hit-count',
        method: 'post',
        data: data,
      });
    },

    // 导入翻译CSV
    importTranslateCsv: (data: any) => {
      return request({
        url: getBaseUrl() + 'import/translate-csv',
        method: 'post',
        data: data,
        headers: { 'Content-Type': undefined }
      });
    },

    getTranslateMention:(params:object)=>{
      return request({
        url: getBaseUrl() + 'sql/get_mention',
        method: 'get',
        params: params,
      });
    },

    getTaskProgress: (taskId: string) => {
      return request({
        url: `${getBaseUrl()}progress/${taskId}`,
        method: 'get',
      });
    },
  };
}
    